package xyz.jpcr.covid19app.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;
import org.w3c.dom.Text;

import java.util.ArrayList;

import xyz.jpcr.covid19app.Activity.PerCityData;
import xyz.jpcr.covid19app.Activity.PerProvinceData;
import xyz.jpcr.covid19app.Model.Coll;
import xyz.jpcr.covid19app.Model.Country.Cities;
import xyz.jpcr.covid19app.Model.Country.CountryResults;
import xyz.jpcr.covid19app.R;
import xyz.jpcr.covid19app.Util.IStoreUtil;

import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_CONFIRMED;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_CURED;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_CURRENT_CONFIRMED;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_DEAD;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_LAST_UPDATE_TIME;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_NAME;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_SUSPECTED;
import static xyz.jpcr.covid19app.Adapter.ProvinceAdapter.PROVINCE_CONFIRMED;
import static xyz.jpcr.covid19app.Adapter.ProvinceAdapter.PROVINCE_CURED;
import static xyz.jpcr.covid19app.Adapter.ProvinceAdapter.PROVINCE_CURRENT_CONFIRMED;
import static xyz.jpcr.covid19app.Adapter.ProvinceAdapter.PROVINCE_DEAD;
import static xyz.jpcr.covid19app.Adapter.ProvinceAdapter.PROVINCE_LAST_UPDATE_TIME;
import static xyz.jpcr.covid19app.Adapter.ProvinceAdapter.PROVINCE_NAME;
import static xyz.jpcr.covid19app.Adapter.ProvinceAdapter.PROVINCE_SUSPECTED;

public class CollectionAdapter extends RecyclerView.Adapter<CollectionAdapter.ViewHolder> {

    private Context mContext;
    private ArrayList<Coll> collArrayList;

    public CollectionAdapter(Context mContext, ArrayList<Coll> collArrayList) {
        this.mContext = mContext;
        this.collArrayList = collArrayList;
    }

    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.activity_collection_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Coll clickedItem = collArrayList.get(position);
                Intent intent = new Intent(mContext, PerCityData.class);
                intent.putExtra(CITY_NAME, clickedItem.getName());
                intent.putExtra(CITY_CURRENT_CONFIRMED, clickedItem.getCurrentConfirmedCount());
                intent.putExtra(CITY_SUSPECTED, clickedItem.getSuspectedCount());
                intent.putExtra(CITY_CURED, clickedItem.getCuredCount());
                intent.putExtra(CITY_DEAD, clickedItem.getDeadCount());
                intent.putExtra(CITY_CONFIRMED, clickedItem.getConfirmedCount());
                intent.putExtra(CITY_LAST_UPDATE_TIME, clickedItem.getUpdateTime());
                mContext.startActivity(intent);
            }
        });
        holder.tv.setText(collArrayList.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return collArrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv;

        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.collection_name_tv);
        }
    }
}
